home *** CD-ROM | disk | FTP | other *** search
/ Die Speccy' 97 / Die Speccy' 97.iso / amiga_system / the_aminet / util / boot / bind_namesii.lha / Dos.c < prev    next >
C/C++ Source or Header  |  1995-09-14  |  4KB  |  225 lines

  1. /* $Header: Src/rcs/Dos.c,v 1.3 1995/02/01 17:49:46 cmh Exp cmh $
  2.  *
  3.  * BindNamesII: Handy assign/path maker.
  4.  * Copyright (C) 1994-95  Magnus Holmgren <cmh@augs.se>
  5.  *
  6.  * This program is free software; you can redistribute it and/or modify
  7.  * it under the terms of the GNU General Public License as published by
  8.  * the Free Software Foundation; either version 2 of the License, or
  9.  * (at your option) any later version.
  10.  *
  11.  * This program is distributed in the hope that it will be useful,
  12.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.  * GNU General Public License for more details.
  15.  *
  16.  * You should have received a copy of the GNU General Public License
  17.  * along with this program; if not, write to the Free Software
  18.  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19.  */
  20. #include <exec/types.h>
  21.  
  22. #ifndef __GNUC__
  23. #include <clib/dos_protos.h>
  24. #include <clib/exec_protos.h>
  25. #include <clib/utility_protos.h>
  26. #endif
  27.  
  28. #ifdef __GNUC__
  29. #include <inline/dos.h>
  30. #include <inline/exec.h>
  31. #include <inline/utility.h>
  32. #endif
  33.  
  34. #include <dos/dosextens.h>
  35. #include <exec/memory.h>
  36.  
  37. #include <string.h>
  38.  
  39. #define Prototype extern
  40.  
  41. #include "assignnode.h"    /* Just to keep Gcc silent.. */
  42.  
  43. #include "proto.h"
  44.  
  45.  
  46. Prototype VOID PrintError( STRPTR, ... );
  47. VOID
  48. PrintError( STRPTR header, ... )
  49. {
  50.     LONG    code = IoErr();
  51.  
  52.     PutStr( "BindNamesII: " );
  53.     VPrintf( header, &header + 1 );
  54.     PutStr( ":\n" );
  55.     PrintFault( code, NULL );
  56. }
  57.  
  58.  
  59. Prototype VOID PrintNoMem( VOID );
  60. VOID
  61. PrintNoMem( VOID )
  62. {
  63.     PutStr( "BindNamesII: Not enough memory\n" );
  64.     SetIoErr( ERROR_NO_FREE_STORE );
  65. }
  66.  
  67.  
  68. /* Return TRUE if the path refers to a "known" volume. */
  69. Prototype LONG HaveProc( STRPTR );
  70. LONG
  71. HaveProc( STRPTR name )
  72. {
  73.     struct DevProc    *proc;
  74.  
  75.     if( ( proc = GetDeviceProc( name, NULL ) ) )
  76.     {
  77.         FreeDeviceProc( proc );
  78.     }
  79.  
  80.     return( ( LONG ) proc );
  81. }
  82.  
  83.  
  84. /* Create the specified directory, including any parent directories that
  85.  * might be needed. Doesn't return any lock, rather a success.
  86.  */
  87. Prototype LONG CreateDirPath( STRPTR );
  88. LONG
  89. CreateDirPath( STRPTR path )
  90. {
  91.     if( Test )
  92.     {
  93.         return( TRUE );
  94.     }
  95.     else
  96.     {
  97.         STRPTR    end;
  98.         BPTR    lock;
  99.  
  100.         end = path + strlen( path ) - 1;
  101.  
  102.         /* Remove trailing slashes */
  103.         if( *end == '/' )
  104.         {
  105.             *end = 0;
  106.         }
  107.  
  108.         /* Does the drawer exist? */
  109.         if( !( lock = Lock( path, SHARED_LOCK ) ) )
  110.         {
  111.             /* No, try to create its parent. */
  112.             end = PathPart( path );
  113.  
  114.             /* Reached the "root"? */
  115.             if( ( end != path ) && *end )
  116.             {
  117.                 TEXT    oldChar;
  118.  
  119.                 oldChar = *end;
  120.                 *end = 0;
  121.  
  122.                 /* Create the parent(s) */
  123.                 if( CreateDirPath( path ) )
  124.                 {
  125.                     *end = oldChar;
  126.                     /* And create the current level */
  127.                     lock = CreateDir( path );
  128.  
  129.                     if( lock )
  130.                     {
  131.                         UnLock( lock );
  132.                     }
  133.                 }
  134.  
  135.                 *end = oldChar;
  136.             }
  137.         }
  138.         else
  139.         {
  140.             UnLock( lock );
  141.         }
  142.  
  143.         return( lock );
  144.     }
  145. }
  146.  
  147.  
  148. /* Structure the command search list is made of */
  149. struct PathEntry
  150. {
  151.     BPTR    Next;    /* Next path in list */
  152.     BPTR    Lock;    /* Lock to drawer */
  153. };
  154.  
  155.  
  156. /* Get a pointer to the current CommandLineInterface struct */
  157. struct CommandLineInterface *GetCLI( VOID )
  158. {
  159.     return( BADDR( ( ( struct Process * ) FindTask( NULL ) )->pr_CLI ) );
  160. }
  161.  
  162.  
  163. /* Clear the command search path for the current process */
  164. Prototype VOID ClearProcPath( VOID );
  165. VOID
  166. ClearProcPath( VOID )
  167. {
  168.     struct CommandLineInterface    *cli = GetCLI();
  169.     struct PathEntry        *path;
  170.     BPTR    next = cli->cli_CommandDir;
  171.  
  172.     cli->cli_CommandDir = NULL;
  173.  
  174.     while( next )
  175.     {
  176.         path = BADDR( next );
  177.         next = path->Next;
  178.  
  179.         if( path->Lock )
  180.         {
  181.             UnLock( path->Lock );
  182.         }
  183.  
  184.         FreeVec( path );
  185.     }
  186. }
  187.  
  188.  
  189. /* Add the specified drawer to the current process' command search list */
  190. Prototype BOOL AddNameProcPath( STRPTR );
  191. BOOL
  192. AddNameProcPath( STRPTR name )
  193. {
  194.     struct PathEntry    *path;
  195.     BOOL    ret = FALSE;
  196.  
  197.     if( ( path = AllocVec( 8, MEMF_ANY | MEMF_CLEAR ) ) )
  198.     {
  199.         if( ( path->Lock = Lock( name, SHARED_LOCK ) ) )
  200.         {
  201.             STATIC struct PathEntry    *last;
  202.  
  203.             if( !last )
  204.             {
  205.                 last = ( struct PathEntry * ) ( &( GetCLI()->cli_CommandDir ) );
  206.  
  207.                 while( last->Next )
  208.                 {
  209.                     last = BADDR( last->Next );
  210.                 }
  211.             }
  212.  
  213.             last->Next = MKBADDR( path );
  214.             last = path;    /* Cache value */
  215.             ret = TRUE;
  216.         }
  217.         else
  218.         {
  219.             FreeVec( path );
  220.         }
  221.     }
  222.  
  223.     return( ret );
  224. }
  225.